# -*- coding: UTF-8 -*-

"""
    日期：20170330
    功能：etl脚本
    描述：读取 包头道路rtic对照表_20170329.csv 文件
         区分rtic对应的 国道、省道、县道
"""

import pymongo
import codecs
import sys
import traceback
reload(sys)
sys.setdefaultencoding('utf-8')

db_name = "baotou_traffic"
roadrtic_col = "roadrtic"


def read_csv():
    with codecs.open("baotou_rtic_20170329.csv", "r", "utf-8") as reader, pymongo.MongoClient("123.207.109.239", 27017) as conn:
        col = conn[db_name][roadrtic_col]
        col.drop()

        bulk = []
        total = 0
        for line in reader.readlines():
            line = line.replace("\r\n", "")
            data = line.split(",")
            if data[0].__contains__("旧"):
                continue
            road_name = strQ2B(data[0])
            road_type = road_name[0:1]
            direction = data[1]
            rticid = data[2]
            # print road_name, road_type, direction, rticid

            obj = {"rtic_id": rticid, "direction": direction, "road_name": road_name, "road_type": road_type}
            bulk.append(obj)
            total += 1

            if total % 500 == 0:
                col.insert(bulk)
                bulk = []
                print "save mongo count: %s" % total

        if len(bulk) > 0:
            col.insert(bulk)
            print "save mongo total: %s" % total

    print "process done!"


def strQ2B(ustring):
    if not ustring :return ""
    '''全角转半角'''
    rstring = ""
    for uchar in ustring:
        inside_code=ord(uchar)
        if inside_code == 12288:                              #全角空格直接转换
            inside_code = 32
        elif (inside_code >= 65281 and inside_code <= 65374): #全角字符（除空格）根据关系转化
            inside_code -= 65248
        rstring += unichr(inside_code)
    return rstring


if __name__ == "__main__":
    read_csv()